// Copyright (c) 2021 Presto Labs Pte. Ltd.
// Author: gekim

#pragma once

#include <map>
#include <string>
#include <vector>

#include "coin2/exchange/base/executor_util/market_ts.h"
#include "coin2/exchange/base/feed/processor.h"
#include "coin2/exchange/binanceus/symbology/product.h"

namespace coin2::exchange::binance_us::feed_v1 {
namespace impl {

class BinanceUsRequestGenerator : public base::feed::RequestGenerator {
 public:
  std::vector<std::string> GenerateRequests(
    const std::vector<std::string>& channels,
    const std::vector<std::string>& symbols,
    const std::map<std::string, std::string>& channel_templates,
    const std::string& request_template) override {
    std::vector<std::string> reqs;

    std::vector<std::string> ch = channels;
    auto it = std::find(ch.begin(), ch.end(), "nav");
    if (it != ch.end()) {
      auto timestamp = MarketTimestampProvider::GetTimestamp();
      for (auto& symbol : symbols) {
        auto product = symbology::BinanceUsProduct::FromNativeStr(symbol, timestamp);
        for (auto nav_type : {"UP", "DOWN"}) {
          if (boost::algorithm::ends_with(product.base().currency(), nav_type)) {
            std::string c = channel_templates.at("nav");
            boost::replace_all(c, "{}", product.nav_native_symbol());
            nlohmann::json j;
            j.push_back(c);
            std::string s = request_template;
            boost::replace_all(s, "{json_list}", j.dump());
            reqs.push_back(s);
          }
        }
      }
      ch.erase(it);
    }

    std::vector<std::string> reqs_others = RequestGenerator::GenerateRequests(
        ch, symbols, channel_templates, request_template);
    reqs.insert(reqs.end(), reqs_others.begin(), reqs_others.end());

    return reqs;
  }
};

}  // namespace impl
using impl::BinanceUsRequestGenerator;
}  // namespace coin2::exchange::binance_us::feed_v1
